package com.opencloud.ywt.provider.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.opencloud.common.model.PageParams;
import com.opencloud.common.model.ResultBody;
import com.opencloud.ywt.client.model.entity.*;
import com.opencloud.ywt.provider.service.BhyfBodyService;
import com.opencloud.ywt.provider.service.BhyfCityService;
import com.opencloud.ywt.provider.service.BhyfStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 园务通（城市）
 * @author yeyong
 */
@Slf4j
@RestController
@Api(value = "园务通", tags = "园务通")
public class BhyfBodyController {
    @Autowired
    private BhyfBodyService bhyfBodyService;

    @Autowired
    private BhyfStudentService studentService;

    /**
     * 获取身体发育数据统计
     * @param gid
     * @return
     */
    @ApiOperation(value = "获取身体发育数据统计", notes = "获取身体发育数据统计")
    @GetMapping("/body/tongji")
    public ResultBody<List<BhyfBflBody>> getBhyfBodyTongJi(
            @RequestParam(required = true) Integer gid,
            @RequestParam(required = true) String starttime,
            @RequestParam(required = true) String endtime
    )
    {
        return ResultBody.ok().data(bhyfBodyService.findBflBodyTongJiList(gid,starttime,endtime));
    }


    /**
     * 获取身体发育分析结果
     * @param gid
     * @param starttime
     * @param endtime
     * @return
     */
    @ApiOperation(value = "获取身体发育分析结果", notes = "获取身体发育分析结果")
    @GetMapping("/body/resultlist")
    public ResultBody<List<BhyfBflBodyInfo>> getBhyfBflBodyResultList(
            @RequestParam(required = true) Integer gid,
            @RequestParam(required = false) Integer gradeid,
            @RequestParam(required = false) Integer classid,
            @RequestParam(required = true) String starttime,
            @RequestParam(required = true) String endtime
    )
    {
        //身体发育数据 按月每月最新的列表
        Integer year = DateTime.now().year();
        Integer month = DateTime.now().month()+1;
        String startDate = "";String endDate = "";

        String session = "上学期";
        if(month>=9 && month<=12){
            session = "上学期";
            startDate = year + "-09-01";
            endDate = year+1+"-02-28";
        }
        if(month>=1 && month<=2){
            session = "上学期";
            startDate = year-1 + "-09-01";
            endDate = year+"-02-28";
        }
        if(month>=3 && month<=8)
        {
            session = "下学期";
            startDate = year + "-03-01";
            endDate = year+"-08-31";
        }
        //log.error("session:"+session+"---startdate:"+startDate+"---month:"+month);
        List<BhyfBflBodyInfo> bhyfBflBodyInfos = new ArrayList<>();
        for(int j=0;j<6;j++)
        {
            List<BhyfBflBodyInfo> tt = new ArrayList<>();
            Date sss = DateUtil.offsetMonth(DateUtil.parseDate(startDate),j);
            String str_sss = DateUtil.formatDate(sss);
            Date eee = DateUtil.offsetMonth(sss,1);
            String str_eee = DateUtil.formatDate(DateUtil.offsetDay(eee,-1));
            tt = bhyfBodyService.findBflBodyInfoList(gid,gradeid,classid,str_sss,str_eee,0);
            //log.error("str_sss:"+str_sss+"---str_eee:"+str_eee+"----count:"+tt.size());
            bhyfBflBodyInfos.addAll(tt);
        }
        bhyfBflBodyInfos = getCompareResult(bhyfBflBodyInfos);
        return ResultBody.ok().data(bhyfBflBodyInfos);
    }


    /**
     * 获取学生每个月的结果
     * @param personid
     * @param starttime
     * @param endtime
     * @return
     */
    @ApiOperation(value = "获取学生每个月的结果", notes = "获取学生每个月的结果")
    @GetMapping("/body/personbodymonthlist")
    public ResultBody<List<BhyfBflBodyInfo>> getBhyfPersonBodyMonthList(
            @RequestParam(required = true) Integer personid,
            @RequestParam(required = true) String starttime,
            @RequestParam(required = true) String endtime
    )
    {
        List<BhyfBflBodyInfo> bhyfBflBodyInfos = bhyfBodyService.findPersonBodyMonthList(starttime,endtime,personid);
        bhyfBflBodyInfos = getCompareResult(bhyfBflBodyInfos);
        return ResultBody.ok().data(bhyfBflBodyInfos);
    }

    /**
     * 获取学生身体发育数据的结果
     * @param gid
     * @param classid
     * @param studentname
     * @param personid
     * @return
     */
    @ApiOperation(value = "获取学生身体发育数据的结果", notes = "获取学生身体发育数据的结果")
    @RequestMapping(value="/ywt/xhbbodypersonlist",method= RequestMethod.GET)
    public ResultBody<List<BhyfStudentBodyInfo>> getXhbBodyPersonList(
            @RequestParam(value = "gid",required = true) Integer gid,
            @RequestParam(value = "classid",required = false) Integer classid,
            @RequestParam(required = true) String starttime,
            @RequestParam(required = true) String endtime,
            @RequestParam(value = "studentname",required = false) String studentname,
            @RequestParam(value = "personid",required = false) Integer personid
    )
    {
        List<BhyfStudentBodyInfo> returnResult = new ArrayList<>();
        if(studentname==null)studentname="";
        List<BhyfStudent> students = studentService.getBhyfStudentList(gid,0,classid,studentname,personid);
        for(int i=0;i<students.size();i++) {
            BhyfStudent student = students.get(i);
            //学生信息
            BhyfStudentBodyInfo studentBodyInfo = new BhyfStudentBodyInfo();
            studentBodyInfo.setStudent(student);
            //身体发育数据 按月每月最新的列表
            List<BhyfBflBodyInfo> bhyfBflBodyInfos = new ArrayList<>();
            bhyfBflBodyInfos = bhyfBodyService.findBflBodyInfoList(gid,0,student.getClassid(),starttime,endtime,student.getPersonid());
            bhyfBflBodyInfos = getCompareResult(bhyfBflBodyInfos);
            studentBodyInfo.setBhyfBflBodyInfos(bhyfBflBodyInfos);
            //关注状态
            /**
            List<BhyfXhbWatch> watches = studentService.findBhyfXhbWatchList(student.getPersonid(), -1,3);
            if (watches != null && watches.size() > 0) {
                studentBodyInfo.setWatchstatus(watches.get(0).getWatchstatus());
                studentBodyInfo.setWatchid(watches.get(0).getId());
            }
             **/
            returnResult.add(studentBodyInfo);
        }
        //log.error("学生："+returnResult.size());
        return ResultBody.ok().data(returnResult);
    }


    public List<BhyfBflBodyInfo> getCompareResult(List<BhyfBflBodyInfo> bhyfBflBodyInfos)
    {
        for(int i=0;i<bhyfBflBodyInfos.size();i++)
        {
            BhyfBflBodyInfo m = bhyfBflBodyInfos.get(i);
            BhyfXhbWhoHw height_hw = bhyfBodyService.getBhyfXhbWhoHw(m.getGender(),1,m.getMonthage());
            BhyfXhbWhoHw weight_hw = bhyfBodyService.getBhyfXhbWhoHw(m.getGender(),2,m.getMonthage());
            BhyfXhbWhoBmi bmi = bhyfBodyService.getBhyfXhbWhoBmi(m.getGender(),m.getHeight());
            //开始比较
            String heightResult = "正常";
            String weightResult = "正常";
            String bmiResult = "正常";
            if(height_hw!=null){
                m.setBad_height(height_hw.getFsd2());
                m.setGood_height(height_hw.getAvg());
                if(m.getHeight()<=height_hw.getFsd2()){
                    heightResult = "迟缓";
                }
            }else{
                heightResult = "未知";
            }
            m.setHeightresult(heightResult);
            if(weight_hw!=null) {
                m.setBad_weight(weight_hw.getFsd2());
                m.setGood_weight(weight_hw.getAvg());
                if (m.getWeight() <= weight_hw.getFsd2()) {
                    weightResult = "低体重";
                }
            }else {
                weightResult = "未知";
            }
            m.setWeightresult(weightResult);
            if(bmi!=null) {
                m.setBmi(bmi);
                if(m.getWeight()<=bmi.getFsd2()){
                    bmiResult = "消瘦";
                }else {
                    double bmivalue = (m.getWeight() - bmi.getAvg()) / bmi.getAvg() * 100;
                    if (bmivalue > 15 && bmivalue<20) {
                        bmiResult = "超重";
                    } else if (bmivalue >= 20  && bmivalue<30) {
                        bmiResult = "轻度肥胖";
                    } else if (bmivalue >=30  && bmivalue<50) {
                        bmiResult = "中度肥胖";
                    } else if (bmivalue >=50) {
                        bmiResult = "重度肥胖";
                    }
                }
            }else {
                bmiResult = "未知";
            }
            m.setBmiresult(bmiResult);
        }
        return bhyfBflBodyInfos;
    }
}
